package com.gxa.controller;

import com.gxa.dto.MyParam;
import com.gxa.dto.ResultDto;
import com.gxa.exception.SystemException;
import com.gxa.pojo.User;
import com.gxa.service.UserService;
import com.gxa.util.MD5Util;
import com.gxa.util.Response;
import com.gxa.util.SMSUtil;
import com.gxa.validate.MyValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private MyValidator validator;

    @Resource(type = SMSUtil.class)
    private SMSUtil smsUtil;

    @GetMapping("/list")
    public String listPage(){
        return "admin/list";
    }

    /**
     * 根据条件查找用户
     * @param user
     * @param myParam
     * @return
     */
    @PostMapping("/findAdmin")
    @ResponseBody
    public ResultDto findUserByParam(User user, MyParam myParam){
        return userService.findUserByParam(user, myParam);
    }

    /**
     * 删除用户的方法
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultDto delete(@RequestParam("ids[]") List<String> ids){
        return userService.delte(ids);
    }

    /**
     * 编辑管理员页面
     * @param userId
     * @return
     */
    @GetMapping("/edit/page")
    public String editPage(@RequestParam("userId") Integer userId, HttpSession session){
        User user = userService.findByPK(userId);
        session.setAttribute("editUser", user);
        return "admin/edit";
    }

    /**
     * 编辑会员信息
     * @param user
     * @param oldPassword
     * @param session
     * @return
     */
    @PostMapping("/edit/do")
    @ResponseBody
    public ResultDto changePassword(User user, String oldPassword, HttpSession session){
        validator.validate(user);
        if (user.getUserId() == null){
            return Response.error(1001, "数据参数有误！");
        }
        if (oldPassword != null && !oldPassword.equals("")) {
            User user1 = userService.findByPK(user.getUserId());
            if (!user1.getUserPassword().equals(MD5Util.MD55(oldPassword))) {
                return Response.error(1002, "原密码输入错误！");
            }
        }
        user.setUserPassword(MD5Util.MD55(user.getUserPassword()));
        Integer update = userService.update(user);
        if (update<=0){
            return Response.error(1002, "修改失败！");
        }
        User byPK = userService.findByPK(user.getUserId());
        session.setAttribute("user", byPK);
        return Response.success("修改成功！");
    }

    /**
     * 添加管理员界面
     * @return
     */
    @GetMapping("/add/page")
    public String addPage(){
        return "admin/add";
    }

    @PostMapping("/add/do")
    public ResultDto add(User user){
        validator.validate(user);
        user.setUserPassword(MD5Util.MD55(user.getUserPassword()));
        user.setUserCreateTime(new Timestamp(System.currentTimeMillis()));
        user.setUserUpdateTime(new Timestamp(System.currentTimeMillis()));
        return userService.add(user);
    }

    /**
     * 发送验证码
     * @param userPhone
     * @param session
     * @return
     */
    @PostMapping("/add/sendVerifyCode")
    @ResponseBody
    public ResultDto sendVerifyCode(String userPhone, HttpSession session){
        User byPhone = userService.findByPhone(userPhone);
        if (byPhone!=null){
            throw new SystemException(1002, "该用户已存在！");
        }
        String code = smsUtil.getCode();
        if (!smsUtil.sendVerifyCode(userPhone, code)) {
            return Response.error(1001, "验证码发送失败");
        }
        session.setAttribute("verifyPhone", userPhone);
        session.setAttribute("verifyCode", code);
        return Response.success("发送成功！");
    }

    /**
     * 验证验证码是否正确
     * @param user
     * @param verifyCode
     * @param session
     * @return
     */
    @PostMapping("/add/checkVerifyCode")
    @ResponseBody
    public ResultDto checkVerifyCode(User user, String verifyCode, HttpSession session){
        String verifyCode1 = (String) session.getAttribute("verifyCode");
        String verifyPhone = (String) session.getAttribute("verifyPhone");
        if (!user.getUserPhone().equals(verifyPhone)){
            throw new SystemException(1001, "该手机号尚未接收验证码");
        }
        if (!verifyCode.equals(verifyCode1)) {
            throw new SystemException(1002, "验证码不正确！~");
        }
        session.removeAttribute("verifyCode");
        session.removeAttribute("verifyPhone");
        return Response.success("验证成功!");
    }



}
